/**
 * @projectName interview_-assistant
 * @package org.example.service.impl
 * @className org.example.service.impl.CrawlerServiceImpl
 * @copyright Copyright 2025 Thunisoft, Inc All rights reserved.
 */
package org.example.service.impl;

import dev.langchain4j.service.AiServices;
import lombok.extern.slf4j.Slf4j;
import org.example.pojo.entity.Article;
import org.example.service.CrawlerService;
import org.example.service.Interviewer;
import org.example.util.ContentParser;
import org.example.util.CreateModel;
import org.example.util.FileStorage;
import org.example.util.PageFetcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * CrawlerServiceImpl
 * @description
 * @author admin
 * @date 2025/4/14 10:36
 * @version TODO
 */
@Service
@Slf4j
public class CrawlerServiceImpl implements CrawlerService {

    @Autowired
    private PageFetcher pageFetcher;

    @Autowired
    private ContentParser contentParser;
    @Autowired
    private CreateModel createModel;

    @Autowired
    private FileStorage storageService;

    @Value("${crawler.request.interval:2000}")
    private int requestInterval;

    @Value("${crawler.page.interval:3000}")
    private int pageInterval;

    @Value("${crawler.keyword:面试题}")
    private String defaultKeyword;

    @Value("${crawler.max.pages:5}")
    private int defaultMaxPages;

    /**
     * 每24小时执行一次的定时任务
     * 使用cron表达式：0 0 0 * * ? 表示每天0点执行
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void scheduledCrawl() {
        log.info("开始执行定时爬虫任务...");
        try {
            List<Article> articles = crawlArticles(defaultKeyword, defaultMaxPages);
            log.info("定时爬虫任务完成，共爬取{}篇文章", articles.size());
        } catch (Exception e) {
            log.error("定时爬虫任务执行失败", e);
        }
    }

    @Override
    public List<Article> crawlArticles(String keyword, int maxPages) throws RuntimeException {
        List<Article> allArticles = new ArrayList<>();
        try {
            // 爬取所有页面的文章
            for (int page = 1; page <= maxPages; page++) {
                String pageUrl = pageFetcher.buildSearchUrl(keyword, page);
                String html = pageFetcher.fetch(pageUrl);
                List<Article> pageArticles = contentParser.parseArticleList(html);
                
                // 获取每篇文章的详细信息
                for (Article article : pageArticles) {
                    try {
                        String detailHtml = pageFetcher.fetch(article.getUrl());
                        contentParser.fillArticleDetail(detailHtml, article);
                        allArticles.add(article);
                        Thread.sleep(requestInterval);
                    } catch (Exception e) {
                        log.error("Error fetching article detail: " + article.getUrl(), e);
                    }
                }
                
                Thread.sleep(pageInterval);
            }
            for(Article article : allArticles){
                String content = article.getContent();
                Interviewer interviewer = AiServices.builder(Interviewer.class)
                        .chatLanguageModel(createModel.createChatLanguageModel())
                        .build();
                List<List<String>> summarizeCrawlArticleList = interviewer.summarizeCrawlArticle(content);
                for (List<String> stringList : summarizeCrawlArticleList){
                    
                }
            }
            return allArticles;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException("Crawling interrupted", e);
        } catch (IOException e) {
            throw new RuntimeException("Error during crawling", e);
        }
    }
}